#include <iostream>

using namespace std;

typedef long long LL;

const int N = 1010;
LL n, m;
LL a[N][N];
LL ti[N][N];
LL f[N][N];

int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			cin >> a[i][j];
		}
	}
	
    for (int i = 0; i <= n; i++) 
    {
        for (int j = 0; j <= m; j++) 
        {
            f[i][j] = -1e9;
            ti[i][j] = 1e9;
        }
    }
    int tmp; cin >> tmp;
    for(int i = 1; i <= tmp; i++)
    {
        int x, y, v; cin >> x >> y >> v;
        ti[x][y] = v;
    }
	f[0][1] = 0;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			if(i + j - 2 >= ti[i][j]) a[i][j] = -1e9;
		}
	}
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            f[i][j] = max(f[i - 1][j], f[i][j - 1]) + a[i][j];
        }
    }
	LL ret = 0;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			ret = max(ret, f[i][j]);
		}
	}
	cout << ret << endl;
	return 0;
}
